/*******************************
This dofile produces graphs.

-share self-employed
- share only self-employed
- share only in UI
- share of earnings accounted for by self-employment

Figure 1 in paper
**********************************/
    
#delimit ;
set more off ;
include ./config.do ; 
include ./drbvars.do ; 
cap log close ; 
log using $logdir./04.01.selfemp_graphs_$date.log, replace;  
use $datadir/all_earnings_long.dta, replace; 

/* first generate indicators */

gen only_se = (se_earnings >0 & national_earnings == 0) ;
/* flag_se is indicator for self-employment */

gen only_ui = (flag_se == 0 & national_earnings > 0) ;
drop only_instate ;
gen only_instate = (national_earnings==instate_earnings & instate_earnings>0 & flag_se == 0 ); 
gen share_seearnings = (se_earnings/(se_earnings+national_earnings)) ;
replace share_seearnings = . if year_grad + year_postgrad < 2007 ; 

gen share_seearnings_se = share_seearnings ; 
	replace share_seearnings_se= . if flag_se == 0 ; /*earnings conditional on self-emp*/

tab ui_state, m; 
/*********************************************First, overall graphs ***********/
tempfile mainfile ;
save `mainfile', replace ; 


/********************* DOING EVERYTHING BY DEGREE LEVEL *******************/     
                             
use `mainfile', clear ; 
keep if year_postgrad<=10;

gen no_earnings = (flag_se == 0 & national_earnings == 0) ;
gen only_national = (national_earnings > 0 & instate_earnings == 0 );
gen all = 1 ; 

collapse (mean) only_se flag_se only_ui share_seearnings share_seearnings_se only_instate
         (sum) count_only_se=only_se count_flag_se=flag_se count_only_ui=only_ui count_noearnings=no_earnings
                count_only_national=only_national count_only_instate=only_instate count_all=all 

         (count) count_share_seearnings=share_seearnings
                 count_share_seearnings_se=share_seearnings_se , by(year_postgrad deglevl_code) ;

drbvars count_all, gen(round_) counts(count_all) ;

foreach var in only_se flag_se only_ui only_instate { ;
      drbvars count_`var', gen(rounded_) counts(count_`var');
      gen round_`var' = rounded_count_`var'/round_count_all ;
      drbvars round_`var', replace; 
};

drbvars share_seearnings, gen(round_) ;
drbvars share_seearnings_se, gen(round_) ;
drop round_count_all; 
outsheet round_* year_postgrad deglevl_code if deglevl_code == "03" | deglevl_code == "05"
         using $supportdir/share_se_graphs_output_$date.csv, comma replace ;

outsheet count_* year_postgrad deglevl_code if deglevl_code=="03" | deglevl_code =="05"
         using $supportdir/support_share_selfemp_$date.csv, comma replace ;
                             
foreach deglvl in 03 05 { ;

local lab03 Associates;
local lab05 Bachelors;
                             

	twoway (connected rounded_only_se year_postgrad if deglevl_code == "`deglvl'")
       (connected rounded_flag_se year_postgrad if deglevl_code == "`deglvl'")
       (connected rounded_only_ui year_postgrad if deglevl_code == "`deglvl'")
       (connected rounded_only_instate year_postgrad if deglevl_code == "`deglvl'"),
     xtitle("Year Post-Graduation")
     ytitle("Share Employed")
     legend(label(1 "Only Self-Emp") label(2 "Self-Emp") 
		label(3 "Only UI") label(4 "Only In-State UI")) 
     ylabel(0(.1)1) yscale(range(0(0.1)1))
     title("Share Self-Employed, `lab`deglvl''");
     
     graph export "$resultsdir/share_se_ypg_`deglvl'_$date.png", replace; 
     
twoway (connected rounded_share_seearnings year_postgrad if deglevl_code == "`deglvl'")
	(connected rounded_share_seearnings_se year_postgrad if deglevl_code == "`deglvl'"),
     xtitle("Year Post-Graduation")
     ytitle("Share Earnings")
	legend(label(1 "Share Earnings Self-Employed") 
		label(2 "Share Conditional on Self-Employment"))
     title("Share Self-Employment Income , `lab`deglvl''");
     
     graph export "$resultsdir/share_seearnings_`deglvl'_date.png", replace;
};

append using $supportdir/support_share_selfemp_$date.dta ;

save $supportdir/support_share_selfemp_$date.dta, replace ; 

